package com.samoilovich.belarustoday.adapters;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.basefragments.BaseFragmentActivity;
import com.samoilovich.belarustoday.datemodel.CategoryModel;
import com.samoilovich.belarustoday.tools.Utility;

import java.util.ArrayList;

public class CategoryAdapter extends BaseAdapter implements ImageLoadingListener {
    private ArrayList<CategoryModel> mCategories;
    private int mResId;
    private Activity mActivity;
    private DisplayImageOptions mDisplayImageOptions;
    private boolean mPictureLoadingState;
    private int mNoImageId;

    public CategoryAdapter(Activity activity, ArrayList<CategoryModel> categories, int resId, int noImageId) {
        mCategories = categories;
        mResId = resId;
        mActivity = activity;
        mNoImageId = noImageId;
        mPictureLoadingState = Utility.getPictureLoadingState(mActivity);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ALPHA_8;
        mDisplayImageOptions = new DisplayImageOptions.Builder().decodingOptions(options).cacheInMemory().build();
    }

    @Override
    public int getCount() {
        if (mCategories != null) {
            return mCategories.size();
        }

        return 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public CategoryModel getItem(int position) {
        if (mCategories != null && mCategories.size() > position) {
            return mCategories.get(position);
        }

        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        CategoryModel temp;

        if (convertView == null) {
            convertView = LayoutInflater.from(mActivity).inflate(mResId, null);
            holder = new ViewHolder();
            holder.CategoryWait = (ProgressBar) convertView.findViewById(R.id.progressBar_categoryWait);
            holder.Logo = (ImageView) convertView.findViewById(R.id.imageView_logo);
            holder.Title = (TextView) convertView.findViewById(R.id.textView_categoryContentTitle);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        temp = getItem(position);
        holder.CategoryWait.setTag(position);
        holder.Logo.setTag(holder.CategoryWait);
        if (temp.getImageDrawable() != null) {
            holder.Logo.setVisibility(View.VISIBLE);
            holder.CategoryWait.setVisibility(View.GONE);
            holder.Logo.setImageDrawable(temp.getImageDrawable());
        } else {
            if (temp.getImageLink() != null && !temp.getImageLink().equals(Constants.EMPTY_STRING)) {
                holder.Logo.setVisibility(View.GONE);
                holder.CategoryWait.setVisibility(View.VISIBLE);
                if (mPictureLoadingState) {
                    holder.Logo.setVisibility(View.VISIBLE);
                    holder.CategoryWait.setVisibility(View.GONE);
                    ImageLoader.getInstance().denyNetworkDownloads(false);
                    ImageLoader.getInstance().displayImage(temp.getImageLink(), holder.Logo, mDisplayImageOptions, this);
                } else {
                    holder.Logo.setVisibility(View.VISIBLE);
                    holder.CategoryWait.setVisibility(View.GONE);
                    ImageLoader.getInstance().denyNetworkDownloads(true);
                    ImageLoader.getInstance().displayImage(temp.getImageLink(), holder.Logo, mDisplayImageOptions, this);
                }
            } else {
                holder.Logo.setVisibility(View.VISIBLE);
                holder.CategoryWait.setVisibility(View.GONE);
                holder.Logo.setImageResource(mNoImageId);
                temp.setImageDrawable(holder.Logo.getDrawable());
            }
        }

        holder.Title.setText(temp.getName());
        convertView.setTag(holder);

        return convertView;
    }

    public void setCategories(ArrayList<CategoryModel> categories) {
        mCategories = categories;
        notifyDataSetChanged();
    }

    public void setPictureLoadingState(boolean state) {
        mPictureLoadingState = state;
    }

    @Override
    public void onLoadingStarted(String imageUri, View view) {
        mPictureLoadingState = Utility.getPictureLoadingState(mActivity);
        view.setVisibility(View.GONE);
        ((View) view.getTag()).setVisibility(View.VISIBLE);
    }

    @Override
    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
        ImageView image = (ImageView) view;

        image.setImageResource(mNoImageId);
        image.setVisibility(View.VISIBLE);
        ((View) view.getTag()).setVisibility(View.GONE);
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        ImageView image = (ImageView) view;

        View tag = (View) view.getTag();
        Integer position = (Integer) tag.getTag();
        CategoryModel category = getItem(position);

        view.setVisibility(View.VISIBLE);
        tag.setVisibility(View.GONE);
        if (image.getDrawable() != null) {
            category.setImageDrawable(image.getDrawable());
        }
    }

    @Override
    public void onLoadingCancelled(String imageUri, View view) {

    }

    // -----------------------------------------------------------------------------------
    private class ViewHolder {
        ProgressBar CategoryWait;
        ImageView Logo;
        TextView Title;
    }

}
